package org.example.hypersonaltools.tool;

import lombok.Getter;
import lombok.Setter;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.util.Iterator;
import java.util.List;

@Setter
@Getter
public class DeepSeekDataOptionTools {

    private List<Integer> mouseFlag;
    private List<String> key;
    private List<Double[]> point;
    private List<Integer> opt;

    // 使用迭代器保证遍历时删除的安全性
    public void run() {
        try {
            Robot robot = new Robot();
            robot.setAutoDelay(100); // 设置操作之间的默认延迟

            Iterator<Integer> optIterator = opt.iterator();
            while (optIterator.hasNext()) {
                int operation = optIterator.next();

                switch (operation) {
                    case 0: // 鼠标操作
                        if (!mouseFlag.isEmpty()) {
                            int buttonFlag = mouseFlag.get(0);
                            handleMouseEvent(robot, buttonFlag);
                            mouseFlag.remove(0);
                        }
                        break;
                    case 1: // 键盘操作
                        if (!key.isEmpty()) {
                            String keyCombination = key.get(0);
                            handleKeyEvent(robot, keyCombination);
                            key.remove(0);
                        }
                        break;
                    case 2: // 指针移动
                        if (!point.isEmpty()) {
                            Double[] coordinates = point.get(0);
                            handleMouseMove(robot, coordinates);
                            point.remove(0);
                        }
                        break;
                }
                optIterator.remove(); // 移除已处理的操作
                Thread.sleep(500); // 操作间隔
            }
        } catch (AWTException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void handleMouseEvent(Robot robot, int buttonFlag) {
        int buttonMask = 0;
        switch (buttonFlag) {
            case 0:
                buttonMask = InputEvent.BUTTON1_DOWN_MASK;
                break;
            case 1:
                buttonMask = InputEvent.BUTTON3_DOWN_MASK;
                break;
            case 2:
                // 双击处理
                robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
                robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
                robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
                robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
                return;
        }
        robot.mousePress(buttonMask);
        robot.mouseRelease(buttonMask);
    }

    private void handleKeyEvent(Robot robot, String keyCombination) {
        try {
            int keyCode = Integer.parseInt(keyCombination); // 假设存储的是KeyEvent常量值
            robot.keyPress(keyCode);
            robot.keyRelease(keyCode);
        } catch (NumberFormatException e) {
            System.out.println("Invalid key code: " + keyCombination);
        }
    }

    private void handleMouseMove(Robot robot, Double[] coordinates) {
        if (coordinates.length >= 2) {
            robot.mouseMove(coordinates[0].intValue(), coordinates[1].intValue());
        }
    }

    @Override
    public String toString() {
        return "DeepSeekDataOptionTools{" +
                "key=" + key +
                ", mouseFlag=" + mouseFlag +
                ", point=" + point +
                ", opt=" + opt +
                '}';
    }

    // 其他字段的setter/getter保持类似结构...
}
